<template>
  <div>
    <div style="margin-bottom: 16px; margin: 0 auto">
      <a-row>
        <a-col :span="24">
          <h1 style="text-align: center; color: #337ab7; font-size: 25px">
            <span class="glyphicon glyphicon-search" aria-hidden="true"></span
            ><span style="text-shadow: 0px 0px 4px grey"
              ><a href="javascript:;">{{ entercon }}</a></span
            >
          </h1>
        </a-col>
      </a-row>
            <v-nave></v-nave>
      <a-row type="flex" justify="center">
        <a-col :xs="23" :sm="23" :md="18" :lg="18" :xl="18">
          <a-input-search
            :placeholder="searchWords == '在这里输入关键词' ?searchWords:'' "
              :value="searchWords == '在这里输入关键词' ?'':searchWords "
            enter-button="检索"
            :allowClear="true"
            @search="SearchAction"
            @change="SearchChange"
          >
            <a-select
              slot="addonBefore"
              :default-value="search_keys[0].id"
              style="width: 120px"
              @change="SearchQsFieldChange"
            >
              <a-select-option
                :value="field.id"
                v-for="(field, index) in search_keys"
                :key="index"
              >
                {{ field.name }}
              </a-select-option>
            </a-select>
          </a-input-search>
        </a-col>
      </a-row>

    </div>
    <div style="margin-top: 16px"></div>
  </div>
</template>
<script>
import VNave from '../../../../components/common/searchnav';
export default {
  data() {
    return {
      // 搜索
      searchWords:'在这里输入关键词',
      search_fields_adv: [],
      formLayout: "horizontal",
      form: this.$form.createForm(this, { name: "coordinated" }),
      visible: false,
      entercon: "电子期刊检索",
    };
  },
  components: {VNave},
  watch: {
  },
  computed: {
    //   search
    search_keys() {
      return this.$store.state[this.$store.state.Namedparent.name].books
        .fieldsAllow.searchKeys;
    },
    search_fields() {
      return this.$store.state[this.$store.state.Namedparent.name].books
        .fieldsAllow.sortFields;
    },
  },
  mounted() {
    //  console.log(this.$store.state.Namedparent)
    //   this.$store.state.Namedparent.key == 0 ? (this.entercon ="统一检索")
    //           : this.$store.state.Namedparent.key == 1 ? (this.entercon ="检索纸书")
    //           : this.$store.state.Namedparent.key == 2 ? (this.entercon ="检索电子书")
    //           : (this.entercon ="检索电子期刊")
    //           console.log(this.$route.query.searchtitle)
    //           if(this.$route.query.searchtitle && this.$route.name !='detail'){
    //             this.SearchAction(this.$route.query.searchtitle)
    //           }
    if(this.$route.query.searchtitle){
      this.searchWords = this.$route.query.searchtitle;
    }
  },
  methods: {
    //   search
    fieldTitle(event) {
      var title = event.target.value.trim();
      if (this.search_fields_adv.length == 0) {
        this.search_fields_adv.push({
          fieldCode: this.$store.state[this.$store.state.Namedparent.name].books
            .fieldsAllow.searchAdvCode.title,
          fieldValue: title,
        });
      } else {
        var len = this.search_fields_adv.length;
        var find = false;

        for (var i = 0; i < len; i++) {
          if (
            this.search_fields_adv[i].fieldCode ==
            this.$store.state[this.$store.state.Namedparent.name].books
              .fieldsAllow.searchAdvCode.title
          ) {
            this.search_fields_adv[i].fieldValue = title;
            if (title.length == 0) {
              this.search_fields_adv.splice(i, 1);
              len--;
            }
            find = true;
          } 
        }
        if (!find) {
          this.search_fields_adv.push({
            fieldCode: this.$store.state[this.$store.state.Namedparent.name]
              .books.fieldsAllow.searchAdvCode.title,
            fieldValue: title,
          });
        }
      }
    },
    fieldAuthor(event) {
      var author = event.target.value;
      if (this.search_fields_adv.length == 0) {
        this.search_fields_adv.push({
          fieldCode: this.$store.state[this.$store.state.Namedparent.name].books
            .fieldsAllow.searchAdvCode.author,
          fieldValue: author,
        });
      } else {
        var len = this.search_fields_adv.length;
        var find = false;
        for (var i = 0; i < len; i++) {
          if (
            this.search_fields_adv[i].fieldCode ==
            this.$store.state[this.$store.state.Namedparent.name].books
              .fieldsAllow.searchAdvCode.author
          ) {
            this.search_fields_adv[i].fieldValue = author;
            if (author.length == 0) {
              this.search_fields_adv.splice(i, 1);
              len--;
            }
            find = true;
          }
        }
        if (!find) {
          this.search_fields_adv.push({
            fieldCode: this.$store.state[this.$store.state.Namedparent.name]
              .books.fieldsAllow.searchAdvCode.author,
            fieldValue: author,
          });
        }
      }
    },
    fieldPublisher(event) {
      var publisher = event.target.value;
      if (this.search_fields_adv.length == 0) {
        this.search_fields_adv.push({
          fieldCode: this.$store.state[this.$store.state.Namedparent.name].books
            .fieldsAllow.searchAdvCode.publisher,
          fieldValue: publisher,
        });
      } else {
        var len = this.search_fields_adv.length;
        var find = false;
        for (var i = 0; i < len; i++) {
          if (
            this.search_fields_adv[i].fieldCode ==
            this.$store.state[this.$store.state.Namedparent.name].books
              .fieldsAllow.searchAdvCode.publisher
          ) {
            this.search_fields_adv[i].fieldValue = publisher;
            if (publisher.length == 0) {
              this.search_fields_adv.splice(i, 1);
              len--;
            }
            find = true;
          }
        }
        if (!find) {
          this.search_fields_adv.push({
            fieldCode: this.$store.state[this.$store.state.Namedparent.name]
              .books.fieldsAllow.searchAdvCode.publisher,
            fieldValue: publisher,
          });
        }
      }
    },
    fieldISBN(event) {
      var isbn = event.target.value;
      if (this.search_fields_adv.length == 0) {
        this.search_fields_adv.push({
          fieldCode: this.$store.state[this.$store.state.Namedparent.name].books
            .fieldsAllow.searchAdvCode.isbn,
          fieldValue: isbn,
        });
      } else {
        var len = this.search_fields_adv.length;
        var find = false;
        for (var i = 0; i < len; i++) {
          if (
            this.search_fields_adv[i].fieldCode ==
            this.$store.state[this.$store.state.Namedparent.name].books
              .fieldsAllow.searchAdvCode.isbn
          ) {
            this.search_fields_adv[i].fieldValue = isbn;
            if (isbn.length == 0) {
              this.search_fields_adv.splice(i, 1);
              len--;
            }
            find = true;
          }
        }
        if (!find) {
          this.search_fields_adv.push({
            fieldCode: this.$store.state[this.$store.state.Namedparent.name]
              .books.fieldsAllow.searchAdvCode.isbn,
            fieldValue: isbn,
          });
        }
      }
    },
    handleAdvancedSearchOk() {
      this.visible = false;
    },
    SearchQsFieldChange: function (search_field) {
      this.$store.commit("ChangeQsSearchField", search_field);
    },
    SearchChange(e){
        const { value } = e.target;
         this.searchWords = value;
    },
    SearchAction: function (search_keyword) {
      if(search_keyword == ''){
        this.$message.error('请输入检索关键字');
        return ;
      }
       this.$store.commit(
            this.$store.state.Namedparent.name + "/searchtype",
            0
          )
      // this.$store.state.books.loading_over = false;
      this.$store.state[this.$store.state.Namedparent.name].loading_over = true;
      this.$store.commit(
        this.$store.state.Namedparent.name + "/UpdateQsSearchKeyWord",
        search_keyword
      );

       this.$store.commit(this.$store.state.Namedparent.name+"/changesearchtitle", search_keyword);
      
      this.$store.commit(this.$store.state.Namedparent.name+"/clearsearchcategoryid", '');
      this.$store
        .dispatch(this.$store.state.Namedparent.name + "/getsiderbar")
        .then(() => {
          this.$store.dispatch(
            this.$store.state.Namedparent.name + "/GetBooks",
            0
          );
        });

      this.$router.push({
        path: "/QikanSearch",
        query: { searchtitle: search_keyword },
      });
      //

      // this.$store.dispatch(getbooks);
      //console.log(this.$store)

      //   this.$router.push("/");
    },
  },
};
</script>